package net.sourceforge.gomoku

import annotation.tailrec

object Cell{
//  private def rotate45(c:Cell):Cell = {
//    import c._
//    Cell(x-y, x+y)
//  }
  /*
  private def rotate45(c:Cell):Cell = {
//    val c1000 = c + Cell(1000,1000)
//    val crot = Cell(c1000.x - c1000.y, c1000.x max c1000.y)
//    val ret = crot + Cell(0,-1000)
//    ret
    //Cell( (c.x-c.y+1)/2, c.x+c.y )

    import c._
    if( x-y >= 2 ){
      rotate45(c + Cell(-1,1)) + Cell(1,0)
    }else if( x-y <= -2 ){
      rotate45(c + Cell(1,-1)) + Cell(-1,0)
    }else if( x==y ){
      Cell(0,x)
    }else if (x==y+1 ){
      Cell(1,y+1)
    }else if(x==y-1){
      Cell(-1,y)
    }else{
      throw new IllegalStateException(""+c)
    }
  }
  */


//  def print45(x:Int, y:Int){
//    val c = Cell(x,y)
//    printf("%s => 45 => %s %n", c, rotate45(c))
//  }
//
//  def main(args:Array[String]){
//    print45(0,0)
//    print45(1,0)
//    print45(1,1)
//    print45(0,1)
//    print45(2,0)
//    print45(2,1)
//    print45(2,2)
//    print45(1,2)
//    print45(0,2)
//  }


  val symmOps = List(
    (c:Cell) => Cell(-c.x, c.y),
    (c:Cell) => Cell(c.x, -c.y),
    (c:Cell) => Cell(c.y, -c.x)

    // 45-degree rotation is possible only in some cases, no generic way
    // maybe rather ideological, not operator way: pattern line crossing,
    // not direct mapping

    //(c:Cell) => Cell(c.x+c.y, c.y-c.x)
    //rotate45 _
    )
  val combinedSymmOps = 0 until (1<<symmOps.size) map { i =>
    var r = (c:Cell) => c
    for( j <- 0 to 3 ){
      if( ((i>>>j) & 1) == 1  ){
        r = symmOps(j) compose r
      }
    }
    r
  }
}

case class Cell(x:Int, y:Int){
  def +(other:Cell) = Cell(x+other.x, y+other.y)
}
